package com.train.system.mapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.train.common.model.page.PageSorted;
import com.train.common.resp.PageResp;
import com.train.mybatis.base.MyBaseMapper;
import com.train.system.dto.trainstation.TrainStationDTO;
import com.train.system.dto.trainstation.TrainStationPageDTO;
import com.train.system.po.TrainStationPO;
import com.train.system.resp.trainstation.TrainStationDetailResp;
import com.train.system.resp.trainstation.TrainStationListResp;
import com.train.system.resp.trainstation.TrainStationPageResp;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author baitiaojun
 * @description 针对表【trainStation(火车车站)】的数据库操作Mapper
 * @createDate 2025-02-20 16:32:30
 * @Entity generator.domain.TrainStation
 */
public interface TrainStationMapper extends MyBaseMapper<TrainStationPO> {

    default PageResp<TrainStationPageResp> pageList(TrainStationPageDTO trainStationPageDTO) {
        LambdaQueryWrapper<TrainStationPO> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(trainStationPageDTO.getCode())) {
            queryWrapper.like(TrainStationPO::getTrainCode, trainStationPageDTO.getCode());
        }
        PageResp<TrainStationPO> trainStationPOPageResp = this.selectPage(
                trainStationPageDTO,
                queryWrapper, new PageSorted(TrainStationPO::getUpdateTime), new PageSorted(TrainStationPO::getCreateTime));
        List<TrainStationPageResp> trainStationPageRespList = BeanUtil.copyToList(trainStationPOPageResp.getData(), TrainStationPageResp.class);
        return new PageResp<>(trainStationPOPageResp.getTotal(), trainStationPageRespList);
    }

    default boolean existsId(Long id) {
        LambdaQueryWrapper<TrainStationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainStationPO::getId, id);
        return this.exists(queryWrapper);
    }

    /**
     * 判断同车次下是否有相同站序或站名
     * @param trainCode
     * @param stationIndex
     * @param name
     * @return
     */
    default boolean exists(String trainCode, Integer stationIndex, String name) {
        LambdaQueryWrapper<TrainStationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainStationPO::getTrainCode, trainCode)
                .and(wrapper -> wrapper.eq(TrainStationPO::getIndex, stationIndex)
                        .or().eq(TrainStationPO::getName, name));
        return this.exists(queryWrapper);
    }

    default List<TrainStationDTO> getTrainStationList(String trainCode) {
        LambdaQueryWrapper<TrainStationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainStationPO::getTrainCode, trainCode);
        List<TrainStationPO> trainStationPOS = this.selectList(queryWrapper);
        return BeanUtil.copyToList(trainStationPOS, TrainStationDTO.class);
    }

    default TrainStationDetailResp getTrainStationDetail(TrainStationDTO trainStationDTO) {
        LambdaQueryWrapper<TrainStationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(TrainStationPO::getName, TrainStationPO::getNamePinyin, TrainStationPO::getInTime,
                        TrainStationPO::getOutTime, TrainStationPO::getKm)
                .eq(StrUtil.isNotEmpty(trainStationDTO.getTrainCode()), TrainStationPO::getTrainCode, trainStationDTO.getTrainCode())
                .eq(ObjectUtil.isNotNull(trainStationDTO.getIndex()), TrainStationPO::getIndex, trainStationDTO.getIndex());
        TrainStationPO trainStationPO = this.selectOne(queryWrapper);
        return BeanUtil.copyProperties(trainStationPO, TrainStationDetailResp.class);
    }

    List<TrainStationPO> selectTrainStationByCode(@Param("trainCodes") List<String> trainCodes);

    default List<TrainStationListResp> getTrainStationListByTrainCodes(List<String> trainCodes) {
        LambdaQueryWrapper<TrainStationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(TrainStationPO::getTrainCode, TrainStationPO::getIndex, TrainStationPO::getName, TrainStationPO::getNamePinyin,
                TrainStationPO::getInTime, TrainStationPO::getOutTime, TrainStationPO::getStopTime, TrainStationPO::getKm)
                .in(TrainStationPO::getTrainCode, trainCodes).orderBy(true, true, TrainStationPO::getIndex);
        return BeanUtil.copyToList(this.selectList(queryWrapper), TrainStationListResp.class);
    }
}